package MultiThreadExample;
public class Treads {
    // 方式1：继承Thread类
    static class MyThread extends Thread {
        private String threadName;
        private int delay;

        public MyThread(String name, int delay) {
            this.threadName = name;
            this.delay = delay;
        }

        @Override
        public void run() {
            // 线程执行的任务
            for (int i = 1; i <= 5; i++) {
                try {
                    // 模拟任务执行时间
                    Thread.sleep(delay);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(threadName + ": " + i);
            }
        }
    }

    // 方式2：实现Runnable接口
    static class MyRunnable implements Runnable {
        private String taskName;
        private int delay;

        public MyRunnable(String name, int delay) {
            this.taskName = name;
            this.delay = delay;
        }

        @Override
        public void run() {
            // 线程执行的任务
            for (int i = 1; i <= 5; i++) {
                try {
                    Thread.sleep(delay);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(taskName + ": " + i);
            }
        }
    }

    public static void main(String[] args) {
        // 使用继承Thread的方式创建线程
        MyThread thread1 = new MyThread("Thread-1", 1000);

        // 使用实现Runnable的方式创建线程
        Thread thread2 = new Thread(new MyRunnable("Thread-2", 1500));

        // 启动线程
        thread1.start();
        thread2.start();

        try {
            // 等待所有线程执行完毕
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("所有线程执行完成！");
    }
}
